<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="false"
      :fetch-data="fetchData"
      :options="options.table"
      @selectionChange="selectionChange"
      :rowClassName="rowClassName"
      :use-action="false"
    >
      <template #actionButton="{ row }"> </template
    ></Table>
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :title="dialogType === 'add' ? '新增证书信息' : '修改证书信息'"
    >
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import DialogVform from "@/components/dialogVform/index";
import SearchForm from "@/components/searchForm/index";
import { stopAndRestart as APIS } from "@/api/zygl/zysb.js";

import formJson from "./inner.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
const props = defineProps({
  parentNode: {
    type: Object,
    default: () => ({}),
  },
});
const plugAdd = () => {
  form.value.pid = props.parentNode.id;
};
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});

const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["certificateName", "certificateCode"].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
// const plugEdit = (row) => {
//   form.value = { ...row };
// };
const table = ref();
const filterForm = ref({});
const {
  onSubmit,
  fetchData,
  handleEdit,
  handleAdd,
  handleDel,
  getList,
  form,
  queryParams,
  dialogType,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  // plugEdit,
  plugAdd,
  filterForm: filterForm.value,
});
onMounted(() => {});

watch(
  () => props.parentNode,
  (val) => {
    filterForm.value.pid = val.id || null;
    getList();
  },
  {
    deep: true,
  }
);
const changeParent = (p) => {
  filterForm.value.pid = p.id || null;
  getList();
};
defineExpose({
  changeParent,
});

const selectList = ref([]);
const selectionChange = (rows) => {
  selectList.value = rows;
};

const rowClassName = ({ row, rowIndex }) => {
  const nowTime = Date.now();
  const endTime = new Date(row.periodEnd).getTime();
  const txTime = new Date(row.remindDate).getTime();
  if (endTime && nowTime >= endTime) {
    return "deepred";
  } else if (txTime && nowTime >= txTime && nowTime <= endTime) {
    return "yellow";
  } else {
    return "";
  }
};
</script>

<style lang="scss" scoped></style>
